<?php

namespace addons\tencentms;

use app\common\library\Menu;
use think\Addons;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Cms\V20190321\CmsClient;
use TencentCloud\Cms\V20190321\Models\TextModerationRequest;
use TencentCloud\Cms\V20190321\Models\ImageModerationRequest;
use think\Loader;

/**
 * 插件
 */
class Tencentms extends Addons
{

    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {

        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {

        return true;
    }

    /**
     * 插件启用方法
     * @return bool
     */
    public function enable()
    {

        return true;
    }

    /**
     * 插件禁用方法
     * @return bool
     */
    public function disable()
    {

        return true;
    }

    /**
     * 添加命名空间
     */
    public function appInit()
    {
        //添加支付包的命名空间
        Loader::addNamespace('TencentCloud', ADDON_PATH . 'tencentms' . DS . 'library' . DS . 'Tencentcloud' . DS);
    }

    /**
     * 实现钩子方法
     * @return mixed
     */
    public function textModeration(&$params)
    {
        $config = get_addon_config('tencentms');
        try {
            $cred = new Credential($config['secretid'], $config['secretkey']);
            $httpProfile = new HttpProfile();
            $endpoint = 'cms.'.$config['apiurl'].'tencentcloudapi.com';
            $httpProfile->setEndpoint($endpoint);

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new CmsClient($cred, $config['region'], $clientProfile);

            $req = new TextModerationRequest();
            $req->fromJsonString($params);

            $result = $client->TextModeration($req);
            return $result->serialize();
        }
        catch(TencentCloudSDKException $e) {
            echo $e;
        }
    }

    public function imageModeration(&$params)
    {
        $config = get_addon_config('tencentms');
        try {
            $cred = new Credential($config['secretid'], $config['secretkey']);
            $httpProfile = new HttpProfile();
            $endpoint = 'cms.'.$config['apiurl'].'tencentcloudapi.com';
            $httpProfile->setEndpoint($endpoint);

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new CmsClient($cred, $config['region'], $clientProfile);

            $req = new ImageModerationRequest();
            $req->fromJsonString($params);

            $result = $client->ImageModeration($req);
            return $result->serialize();
        }
        catch(TencentCloudSDKException $e) {
            echo $e;
        }
    }

}
